﻿using System;
using System.Web.Mvc;
using Ninject;
using Sklep.Domain.Abstract;
using Sklep.Domain.Concrete;
using Sklep.WebUI.Infrastructure.Abstract;
using Sklep.WebUI.Infrastructure.Concrete;

namespace Sklep.WebUI.Infrastructure
{
    public class NinjectControllerFactory : DefaultControllerFactory
    {
        private readonly IKernel _ninjectKernel;

        public NinjectControllerFactory()
        {
            _ninjectKernel = new StandardKernel();
            AddBindings();
        }

        protected override IController GetControllerInstance(System.Web.Routing.RequestContext requestContext, Type controllerType)
        {
            return controllerType == null ? null :
            (IController)_ninjectKernel.Get(controllerType);
        }

        private void AddBindings()
        {
            _ninjectKernel.Bind<IProductRepository>().To<EFProductRepository>();

            var emailSettings = new EmailSettings {WriteAsFile = true};

            _ninjectKernel.Bind<IOrderProcessor>().To<EmailOrderProcessor>().WithConstructorArgument("emailSettings",
                                                                                                     emailSettings);

            _ninjectKernel.Bind<IAuthProvider>().To<FormsAuthProvider>();
        }
    }
}